DataFrame() constructor

Dataframe is a 2-D Array.
Used to create or reconstruct a DataFrame object

DataFrame(data=None, index=None, columns=None, dtype=None, copy=None)
      

any()

check if at least one value in a series or along an axis evaluates to True.

# data.csv
Duration,Pulse,Maxpulse,Calories
45,       117,    148,  406.0
60,       102,    127,  300.5
60,       110,    136,
45,       104,     134, 253.3

import pandas as pd
df = pd.read_csv('data.csv')
all_cols = [col for col in df.columns] 
print(all_cols)       # [Duration,Pulse,Maxpulse,Calories]

cols_with_missing = [col for col in df.columns if df[col].isnull().any()]
print(cols_with_missing)  # [Calories]

describe()

describe(): Returns 8 values for each coloumn
  count: The number of non-null values. (For Duration coloumn, count=3)
  mean: The average value. (For Duration coloumn, 70+60+50/3 = 60)
  std: The standard deviation.
  min: The minimum value. (For duration coloumn, min val=50)
  25%: The 25th percentile (first quartile).
   Imagine sorting each column from lowest to highest value. If you go a quarter way through the list, you'll find a number that is bigger than 25% of the values, ie 25%
  50%: The 50th percentile (median).
  75%: The 75th percentile (third quartile).
  max: The maximum value. (For duration coloumn, max val=70)
# data.csv 
Duration,   Pulse,  Maxpulse,   Calories
60,         110,    130,        409.1
60,         117,    145,        479.0
60,         103,    135,        340.0
,         109,    175,        282.4

import pandas as pd
df = pd.read_csv('data.csv')
print(df.describe())

       Duration       Pulse    Maxpulse    Calories
count       3.0    4.000000    4.000000    4.000000
mean       60.0  109.750000  146.250000  377.625000
std        10.0    5.737305   20.155644   85.148904
min        50.0  103.000000  130.000000  282.400000
25%        55.0  107.500000  133.750000  325.600000
50%        60.0  109.500000  140.000000  374.550000
75%        65.0  111.750000  152.500000  426.575000
max        70.0  117.000000  175.000000  479.000000
      

drop() and dropna()

dropna(): Cleans out missing data (NaN/None). DataFrame with NA entries dropped from it or None if inplace=True.
drop(): Deletes explicit rows or columns by name.

drop() dropna()

# Drops the column named 'Age'
df_dropped = df.drop(columns=['Age']) 

# Drops the row with index label 2
df_dropped = df.drop(index=2) 

Example:
data_frame = 
       Duration       Pulse    Maxpulse    Calories
count       3.0    4.000000    4.000000    4.000000
mean       60.0  109.750000  146.250000  377.625000
std        10.0    5.737305   20.155644   85.148904
min        50.0  103.000000  130.000000  282.400000
25%        55.0  107.500000  133.750000  325.600000
50%        60.0  109.500000  140.000000  374.550000
75%        65.0  111.750000  152.500000  426.575000
max        70.0  117.000000  175.000000  479.000000

// Pulse col will be removed
d = data_frame.drop(['Pulse'], axis=1)
          
dropna() Arguments:
inplace=true. Donot create a new copy. Instead replace original(move constructor)


data.csv
Duration,Pulse,Maxpulse,Calories
45,       117,  148,    406.0
60,       102,  127,    300.5
60,       110,  136,
45,       104,  134,    253.3
                  
Drop all rows with NaN
import pandas as pd
df = pd.read_csv('data.csv')
print(df.dropna())  # OR df.dropna(axis=0)

   Duration  Pulse  Maxpulse  Calories
0        45    117       148     406.0
1        60    102       127     300.5
3        45    104       134     253.3
                  
subset=[]. Only drop if this col has NaN
print(df.dropna(axis=0,subset=['Pulse']))
Duration,Pulse,Maxpulse,Calories
45,       117,  148,    406.0
60,       102,  127,    300.5
60,       110,  136,    NaN
45,       104,  134,    253.3
                  
head returns 1st n rows from dataframe, default is 5

isNull

Creates a dataframe with 1 or 0. if value is present, take it as 0, if NaN or None take it as 1

X_train
rollno  marks
5       NaN
NaN     NaN
10      20

t = (X_train.isnull())
rollno = [0, 1, 0]
marks =  [1, 1, 0]
      

read_csv(), .columns

read_csv(): read the CSV file and create a Dataframe from it
.columns: list the coloumn header in the Dataframe

# data.csv 
Duration,   Pulse,  Maxpulse,   Calories
60,         110,    130,        409.1
60,         117,    145,        479.0
60,         103,    135,        340.0
,         109,    175,        282.4

$ test.py
import pandas as pd
df = pd.read_csv('data.csv')    #read in data frame
print(df.to_string())
print(df.columns)

   Duration  Pulse  Maxpulse  Calories
0      60.0    110       130     409.1
1      60.0    117       145     479.0
2      60.0    103       135     340.0
3       NaN    109       175     282.4

Index(['Duration', 'Pulse', 'Maxpulse', 'Calories'], dtype='object')
      

Features

Selecting some columns from dataframe is called features.

import pandas as pd
df = pd.read_csv('data.csv')
print(df.to_string())
features = ['Duration', 'Pulse']
print(df[features])

    Duration  Pulse  Maxpulse  Calories
0      60.0    110       130     409.1
1      60.0    117       145     479.0
2      60.0    103       135     340.0
3       NaN    109       175     282.4

   Duration  Pulse
0      60.0    110
1      60.0    117
2      60.0    103
      

select_dtypes()


// select_dtypes() filter columns based on their data type.
DataFrame.select_dtypes(include=None, exclude=None)

//exclude=['object']: Give me every column except those labeled as object.
X = melb_predictors.select_dtypes(exclude=['object'])

What is dtype

shape()

Find row, coloumn count of datafram read

import pandas as pd
X_full = pd.read_csv('train.csv', index_col='Id')
rows, cols = X_full.shape
print(f"Rows: {rows}, Columns: {cols}")   //Rows: 1460, Columns: 80
      

sum()

Calculate total sum


Feature A: [False, True, False]
Feature B: [True, True, False]

.sum() calculates:
Feature A: 0 + 1 + 0 = 1
Feature B: 1 + 1 + 0 = 2